//
//  BCMssageViewController.m
//  BangcaiBuy
//
//  Created by Liang Zhang on 16/7/8.
//  Copyright © 2016年 BCB. All rights reserved.
//

#import "BCMessageViewController.h"
#import "BCMessageTitleCollectionViewCell.h"
#import "BCMessageTextCollectionViewCell.h"
#import "BCMessageImageCollectionViewCell.h"
#import "BCMessageDetailCollectionViewCell.h"
#import "BCMessageDetailViewController.h"

@interface BCMessageViewController () <UICollectionViewDelegate,UICollectionViewDataSource>
//主视图
@property (nonatomic, strong) UICollectionView *mainCollectionView;
@end

@implementation BCMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self p_initNavgationBar];
    [self p_initCollectionView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //隐藏导航栏
    self.navigationController.navigationBar.hidden = NO;
    
    if (!self.rdv_tabBarController.tabBarHidden) {
        [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
    }
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.rdv_tabBarController.tabBarHidden) {
        [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - View init
-(void)p_initNavgationBar {
    self.title = @"消息";
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)p_initCollectionView {
    UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init];
    self.mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
    self.mainCollectionView.backgroundColor = [UIColor whiteColor];
    self.mainCollectionView.delegate = self;
    self.mainCollectionView.dataSource = self;
    //设置contentInset属性（上左下右的值）
    self.mainCollectionView.contentInset = UIEdgeInsetsMake(0, 15, 0, 15);
    [self.view addSubview:self.mainCollectionView];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    switch (section) {
        case 0:
        {
            return 2;
        }
            break;
        case 1:
        {
            return 4;
        }
            break;
        case 2:
        {
            return 2;
        }
            break;
            
        default: {
            return 0;
        }
            break;
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.row) {

        case 1:
        {
            static NSString *ID = @"MessageTextCell";
            BCMessageTextCollectionViewCell *cell = [BCMessageTextCollectionViewCell cellWithCollectionView:collectionView];
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
            //临时判断条件，日后根据数据类型判断
            if (indexPath.section == 0 || indexPath.section == 2) {
                cell.underLineImageView.hidden = NO;
            }

            return cell;
        }
            break;
        case 2:
        {
            static NSString *ID = @"MessageImageCell";
            BCMessageImageCollectionViewCell *cell = [BCMessageImageCollectionViewCell cellWithCollectionView:collectionView];
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
            return cell;
        }
            break;
        case 3:
        {
            static NSString *ID = @"MessageDetailCell";
            BCMessageDetailCollectionViewCell *cell = [BCMessageDetailCollectionViewCell cellWithCollectionView:collectionView];
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
            return cell;
        }
            break;
            
        default: {
            static NSString *ID = @"MessageTitleCell";
            BCMessageTitleCollectionViewCell *cell = [BCMessageTitleCollectionViewCell cellWithCollectionView:collectionView];
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
            return cell;
        }
            break;
    }
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{
    UIEdgeInsets top = {15,0,0,0};
    return top;
}

//定义每个UICollectionViewCell 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.row) {
        case 1:
        {
            return CGSizeMake(self.view.frame.size.width-30.f, 56);
        }
            break;
        case 2:
        {
            return CGSizeMake(self.view.frame.size.width-30.f, 150);
        }
            break;
        case 3:
        {
            return CGSizeMake(self.view.frame.size.width-30.f, 48);
        }
            break;
            
        default: {
            return CGSizeMake(self.view.frame.size.width-30.f, 62);
        }
            break;
    }
}

//每个item之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

//每个section中不同的行之间的行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    return 0;
}

//collectionview点击
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (3 == indexPath.row) {
        BCMessageDetailViewController *nextViewController = [[BCMessageDetailViewController alloc] init];
        [self.navigationController pushViewController:nextViewController animated:YES];
    }
}

@end
